package controllers;

import helpers.UrlHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.OwnerDomainModel;
import entities.OwnerDomain;

public class CertificateServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String name = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (name == null || name.isEmpty() || !UrlHelper.checkDomain(name)) {
			response.sendRedirect("/msg/page-not-found");
			return;
		}
		
		OwnerDomainModel ownerData = OwnerDomainModel.getInstance();
		OwnerDomain ownerDomain = ownerData.getByDomain(name);
		
		if (ownerDomain == null) {
			response.sendRedirect("/msg/page-not-found");
			return ;
		}
		
		ownerDomain.setLastUpdate(new Date());
		ownerData.insert(ownerDomain);
		
		response.sendRedirect("/profile");
		return;
	}
}
